﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
using Common;

public partial class system_documentcategorylist : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        PageBegin();
        Response.Expires = -1;

        switch (RequestBase.GetFormString("otype"))
        {
            case "loaddata":
                LoadingData();
                break;
            case "delete":
                Delete();
                break;
        }
    }

    #region 信息绑定
    void LoadingData()
    {
        string ctrl = "~/system/_controls/ctlCategoryList.ascx";

        string SQL = " AND DeletionStateCode = 0";
        if (!string.IsNullOrEmpty(RequestBase.GetFormString("systemviews")))
        {
            SQL += " AND DocumentTypeCode = " + RequestBase.GetFormString("systemviews");
        }

        string SQL_Sort = "";
        if (!string.IsNullOrEmpty(RequestBase.GetFormString("field")))
        {
            SQL_Sort = " ORDER BY " + RequestBase.GetFormString("field") + " " + RequestBase.GetFormString("level") + " ";
        }
        else
        {
            SQL_Sort = "  ORDER BY DocumentTypeCode ASC, LineNum ASC ";
        }

        Control uc_rep = LoadControl(ctrl);

        Repeater rptDoucmentCategoryList = (Repeater)uc_rep.FindControl("rptDoucmentCategoryList");

        DBUtility.Pagination pagination = new DBUtility.Pagination();
        try
        {
            if (!string.IsNullOrEmpty(RequestBase.GetString("index")))
                pagination.Sequence = Convert.ToInt32(RequestBase.GetString("index"));
            else
                pagination.Sequence = 1;

            pagination.ItemUnit = "条记录";
            pagination.SortSentence = SQL_Sort;
            pagination.Primarykey = "CategoryId";
            pagination.SearchField = "*";
            pagination.TableName = "DocumentCategoryBase";
            pagination.PageSize = 50;
            pagination.UrlStr = "";
            pagination.ImagesPath = "../_imgs/grid/";
            pagination.SearchSentence = SQL;

            rptDoucmentCategoryList.DataSource = pagination.GetDataTable();
            rptDoucmentCategoryList.DataBind();

            pagination.PageBarFerrari();
            uc_rep.GetType().GetProperty("PaginationBar").SetValue(uc_rep, pagination.PageBar, null);

            StringBuilder strb = new StringBuilder();
            StringWriter sw = new StringWriter(strb);
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            rptDoucmentCategoryList.RenderControl(htw);

            BLL.AjaxHelper.ResponseWrite(strb.ToString());
        }
        catch (System.Threading.ThreadAbortException err)
        {
            //nothing   to   do   
        }
        catch (Exception err)
        {
            BLL.AjaxHelper.ResponseWrite(err.Message);
        }
    }
    #endregion

    #region 设置
    void Delete()
    {
        string res = "";
        bool bl = Validate("business unit", 1);
        if (!bl)
        {
            res = "您没有删除操作的权限!";
        }
        else
        {
            if (Utils.IsGuid(RequestBase.GetFormString("categoryid")))
            {
                BLL.DocumentCategoryBase.Delete(RequestBase.GetFormString("categoryid"));
                res = "succeeded";
            }
            else
            {
                res = "参数错误, 删除失败!";
            }
        }

        BLL.AjaxHelper.ResponseWrite(res);
    }
    #endregion
}
